<?php
session_start();
$id = $_SESSION['SessionID'];

include('connect.php');

$idList = array();
$friendList = array();
$onlineList = array();
$sql = mysql_query("SELECT * FROM tblfriends WHERE ID LIKE '$id'");

// Speichere alle IDs der Freunde vom aktiven Benutzer
while($row = mysql_fetch_array($sql,MYSQL_ASSOC)) {
	$friendID = $row['FriendID'];
	array_push($idList, $friendID);
}

// Suche alle zugehörigen Benutzernamen der Freunde heraus und stelle sie dar
for($i = 0; $i < count($idList); $i++) {
	$friendID = $idList[$i];
	$sql = mysql_query("SELECT Username FROM tblusers WHERE ID LIKE '$friendID'");
	if(mysql_num_rows($sql) > 0) {
		array_push($friendList, mysql_result($sql, 0));
	}
}

for($i = 0; $i < count($friendList); $i++) {
	$sql = mysql_query("SELECT Username FROM tblusers WHERE Username LIKE '$friendList[$i]' AND Online LIKE '1'");
	if(mysql_num_rows($sql) > 0) {
		array_push($onlineList, mysql_result($sql, 0));
	}
}

// Alphabetische Sortierung case insensetive
natcasesort($onlineList);

for($i = 0; $i < count($onlineList); $i++) {
	echo $onlineList[$i] . " (Online) <br />";
	$del_val = $onlineList[$i];
	if(($key = array_search($del_val, $friendList)) !== false) {
		unset($friendList[$key]);
	}
}

// Alphabetische Sortierung case insensetive
natcasesort($friendList);

// Re-Index für das Array, um "leere Plätze zu löschen"
$friendList = array_values($friendList);

for($i = 0; $i < count($friendList); $i++) {
	echo iconv("ISO-8859-1","UTF-8",$friendList[$i]) . " (Offline) <br />";
}

?>